$.lp = {
    init: function(){
//        $(".follow").live('click',function(e){
//                $(".photo_overlay").fullScreen();
//
//            });
//        $(".photoPost").live('click',function(){
//            
//            var id = $(this).attr("id");
//            var title="";
//            var image = "";
//            var name = "";
//            var username = "";
//            var user_profile_img = "";
//            var uploaded = "";
//            var photograph = "";
//            $.getJSON("photos/xhrPhoto", {
//                id:id
//            }, function(data){
//                var photo = data.photo;
//                var user = data.user;
//                var likes = data.likes;
//                var favorites = data.favs;
//                var did_like = data.did_like;
//                var did_fav = data.did_fav;
//                var comments = data.comments;
//                var connect;
//                var comment_count =0;
//                $.each(comments,function(key,value){
//                    comment_count++;
//                });
//                if(data.connected){
//                    connect = true;
//                }else{
//                    connect = false;
//                }
//                var template = $('#photoOverlay_template').html();
//                var img = '<img src="'+photo.source+'" alt="'+photo.title+'" />';
//                image = img;
//                title = photo.title;
//                uploaded = photo.uploaded;
//                photograph = photo.photograph;
//                username = user.username;
//                name = user.first_name+" "+user.last_name;
//                user_profile_img = '<img src="'+user.profile_pic_thumb+'" alt="'+user.first_name+" "+user.last_name+'" width="70px"/>';
//                var data_ = {
//                    connect:connect,
//                    photo_id: id,
//                    image : image,
//                    title : title,
//                    name:name,
//                    username:username,
//                    profile_img:user_profile_img,
//                    date: uploaded,
//                    photographer : photograph,
//                    likes:likes,
//                    favorites:favorites,
//                    comments:comments,
//                    comment_count:comment_count
//                };
//                if(did_like == 0){
//                    data_.did_like = false;
//                }else{
//                    data_.did_like = true;
//                }
//                if(did_fav == 0){
//                    data_.did_fav = false;
//                }else{
//                    data_.did_fav = true;
//                }
//                var html = Mustache.to_html(template, data_);
//                $('#LeapMainWrapper').after(html);
//                $(".photo_overlay").show();
//                $('.overlay_bg').show();
//            });
//            return false;
//        });
        $(".follow").live('click',function(e){
                var this_ = $(this);
                var follower = $(this).parent(".userInfos").attr('id');
                $.post("/follow/followUser", {
                    follower:follower
                },function(data){
                    if(data){
                        this_.removeClass("follow").addClass("fl").text("Unfollow");
                    }
                });
        });
        $(".fl").live('click',function(e){
                var this_ = $(this);
                var follower = $(this).parent("").attr('id');
                $.post("/follow/unFollowUser", {
                    follower:follower
                },function(data){
                    if(data){
                        this_.removeClass("fl").addClass("follow").text("Follow");
                    }
                });
        });
        $('.close_overlay').live('click',function(){
            $('.photo_overlay').remove();
            $('.overlay_bg').hide();
        });
        $("#FirePhoto").live('click',function(){
            var this_ = $(this);
            var photo_id = $(this).parents("ul").attr("id");
            if(!$(this).hasClass('FireCount_f')){
                $.post("photos/like", {
                    photo_id:photo_id
                },function(){
                    this_.text(parseInt(this_.text())+1);
                    this_.attr('class','FireCount_f');
                });
            }else{
                $.post("photos/dislike", {
                    photo_id:photo_id
                },function(){
                    this_.text(parseInt(this_.text())-1);
                    this_.attr('class','FireCount');
                });
            }
        });
        $("#FavPhoto").live('click',function(){
            var this_ = $(this);
            var photo_id = $(this).parents("ul").attr("id");
            if(!$(this).hasClass('FavCount_f')){
                
                $.post("photos/favorite", {
                    photo_id:photo_id
                },function(){
                    this_.text(parseInt(this_.text())+1);
                    this_.attr('class','FavCount_f');
                });
            }
            else{
                $.post("photos/dlFavorite", {
                    photo_id:photo_id
                },function(){
                    this_.text(parseInt(this_.text())-1);
                    this_.attr('class','FavCount');
                });
            }
        });
        $(document).keypress(function(e) {
            if(e.which == 13) {
            var comment = $(".photocomment").find('.comment').val();
            if(comment !== ""){
                var type = "photo";
                var id = $('.extendphoto').attr('id');
                $.getJSON('/comment/postComment?comment='+comment+'&type='+type+'&id='+id, function(data){
                    var comment = '<li>\n\
<span class="profilePicWrapper" ><img src="'+data.user_profile_pic_thumb+'" width="50" height="50" /></span>\n\
                    <div class="commentInfos">\n\
                        <p class="user_name"><a href="'+data.user_name+'">'+data.user_name+'</a></p>\n\
                        <p class="added">just now</p>\n\
                    </div>\n\
                    <p class="comment_">'+data.comment+'</p>\n\
                    <div class="clearfix">\n\
                    </div>\n\
                </li>';
                    $(".commentstream").find("ul").prepend(comment);
                    $(".comment").val("");
                    var count = parseInt($(".photoAction").find("ul").children(":first-child").find("span").text())+1;
                    $(".photoAction").find("ul").children(":first-child").find("span").text(count);
                });
            }
            }
        });
        $("#likephoto").live('click',function(){
            var this_ = $(this);
            var photo_id = $(this).parents("ul").attr("id");
            if(!$(this).hasClass('likeCount_f')){
                $.post("/photos/like", {
                    photo_id:photo_id
                },function(){
                    this_.text(parseInt(this_.text())+1);
                    this_.attr('class','likeCount_f');
                });
            }else{
                $.post("/photos/dislike", {
                    photo_id:photo_id
                },function(){
                    this_.text(parseInt(this_.text())-1);
                    this_.attr('class','likeCount');
                });
            }
        });
        $("#Favphoto").live('click',function(){
            var this_ = $(this);
            var photo_id = $(this).parents("ul").attr("id");
            if(!$(this).hasClass('FavCount_f')){
                
                $.post("/photos/favorite", {
                    photo_id:photo_id
                },function(){
                    this_.text(parseInt(this_.text())+1);
                    this_.attr('class','FavCount_f');
                });
            }
            else{
                $.post("/photos/dlFavorite", {
                    photo_id:photo_id
                },function(){
                    this_.text(parseInt(this_.text())-1);
                    this_.attr('class','FavCount');
                });
            }
        });
        $(".shareMedia").live('click',function(){
            $(this).next().toggle();
        });
        $("#sharephoto").find('#shareFacebook').live('click',function(){
            FB.init({appId: "264489850252668", status: true, cookie: true});
            var picture = $(this).parents('.singlephoto').find('.photoPost').find('img').attr('src');
            var link = "http://www.youtube.com/watch?v="+$(this).parents('.singlephoto').find('.photoPost').attr('id');
            var message = $(this).parents('.singlephoto').find('.description').text();
            var name = $(this).parents('.singlephoto').find('.title').find('a').text();
            postToFeed(picture,link,message,name);
        });
       
        $("#sharephoto").find('#shareTwitter').live('click',function(){
            var picture = $(this).parents('.singlephoto').find('.photoPost').find('img').attr('src');
            var link = "http://www.youtube.com/watch?v="+$(this).parents('.singlephoto').find('.photoPost').attr('id');
            var message = $(this).parents('.singlephoto').find('.description').text();
            var name = $(this).parents('.singlephoto').find('.title').find('a').text();
            
            getTinyURL(link, function(tinyurl){
                // Do something with tinyurl:
                window.open('http://twitter.com/share?url=' + tinyurl + '&text=' + name + '&via=leap', 'twitterwindow', 'height=450, width=550, top='+($(window).height()/2 - 225) +', left='+$(window).width()/2 +', toolbar=0, location=0, menubar=0, directories=0, scrollbars=0');

            });
            //We trigger a new window with the Twitter dialog, in the middle of the page
        });
    }
}
function ago(now,timestamp){
    
        var difference = now - timestamp;
        if(difference < 60)
            return difference+" seconds ago";
        else{
            difference = Math.round(difference / 60);
            if(difference < 60)
                return difference+" minutes ago";
            else{
                difference = Math.round(difference / 60);
                if(difference < 24)
                    return difference+" hours ago";
                else{
                    difference = Math.round(difference / 24);
                    if(difference < 7)
                        return difference+" days ago";
                    else{
                        difference = Math.round(difference / 7);
                        return difference+" weeks ago";
                    }
                }
            }
        }
    }

function postToFeed(picture,link,message,name) {

    // calling the API ...
    var obj = {
        method: 'feed',
        link: 'https://developers.facebook.com/docs/reference/dialogs/',
        picture: picture,
        name: name,
        caption: link,
        description: message
    };

    function callback(response) {
        //document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
    }

    FB.ui(obj, callback);
}
function getTinyURL(longURL, success) {
 
    // Create unique name for callback function:
    var ud = 'json'+(Math.random()*100).toString().replace(/\./g,''),
    	// Define API URL:
        API = 'http://json-tinyurl.appspot.com/?url=';
 
    // Define a new global function:
    // (which will run the passed 'success' function:
    window[ud]= function(o){success&&success(o.tinyurl);};
 
    // Append new SCRIPT element to BODY with SRC of API:
    document.getElementsByTagName('body')[0].appendChild((function(){
 
        var s = document.createElement('script');
        s.type = 'text/javascript';
        s.src = API + encodeURIComponent(longURL) + '&callback=' + ud;
        return s;
 
    })());
 
}